package com.sgb.kjwl.view.widget.lease.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.reflect.TypeToken;

import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.BottomLeaseLayoutBinding;
import com.sgb.kjwl.model.entity.AskLeaseCommonEntity;
import com.sgb.kjwl.model.entity.AskLeaseFactoryEntity;
import com.sgb.kjwl.model.entity.AskLeaseFarmEntity;
import com.sgb.kjwl.model.entity.AskLeaseLandEntity;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.utils.EditInputFilter;
import com.sgb.kjwl.view.ui.activity.lease.ZulinGaodeSearchActivity;
import com.sgb.kjwl.viewmodel.machine.AskLeaseCommonModel;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.viewmodel.machine.AskLeaseLayoutModel;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 租赁-地图查找-求租发布需求公用界面
 */
public class AskLeaseReleaseView extends RelativeLayout implements ItemInputTextView.OnWholeItemClickListener {

    private Context mContext;
    private BottomLeaseLayoutBinding binding;
    private AskLeaseLayoutModel model;
    private int type = 0;

    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;

    private Calendar selectedDate = Calendar.getInstance();
    private Calendar selectedDate2 = Calendar.getInstance();

    private List<DictEntity3> powerDatas = new ArrayList<>();//供水供电日照
    private List<DictEntity3> sunshineDatas = new ArrayList<>();//日照
    private List<DictEntity3> machineDatas = new ArrayList<>();//机械作业
    private List<DictEntity3> hasNotList = new ArrayList<>();//区域平台等列表
    private List<DictEntity3> allpayway = new ArrayList<>();//支付方式列表
    private List<UnitEntity> priceUnit = new ArrayList<>();//价格单位列表
    private ArrayList<PickSelectEntity> cartypelist = new ArrayList<>();//车辆类型列表

    private List<PickSelectEntity> driverDatas;//操作员列表
    private List<PickSelectEntity> priceMatchUnitDatas = new ArrayList<>();//辆/台 单位列表

    private List<PickSelectEntity> classDatas;
    private List<List<PickSelectEntity>> typeDatas;
    private List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> categoryList;

    private AskLeaseCommonEntity commonEntity = new AskLeaseCommonEntity();//公共属性

    private List<PickSelectEntity> leaseTimeDatas = new ArrayList<>();//租赁时长单位
    private OptionsPickerView leaseTimeOpv;//租赁时长单位选择器

    public AskLeaseReleaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    private void initView() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.bottom_lease_layout, this, true);
        model = new AskLeaseLayoutModel(mContext, this);
        initListener();
        init();
    }

    private void initListener() {
        binding.itvType.setOnWholeItemClickListener(this);
        binding.itvCategory.setOnWholeItemClickListener(this);
        binding.itvArea.setOnWholeItemClickListener(this);
        binding.address.setOnWholeItemClickListener(this);
        binding.timeCourse.setOnWholeItemClickListener(this);
        binding.timeDeadline.setOnWholeItemClickListener(this);
        binding.paymentMethod.setOnWholeItemClickListener(this);
        binding.itvWater.setOnWholeItemClickListener(this);
        binding.itvElectricity.setOnWholeItemClickListener(this);
        binding.itvSunshine.setOnWholeItemClickListener(this);
        binding.itvMachine.setOnWholeItemClickListener(this);
        binding.itvOffice.setOnWholeItemClickListener(this);
        binding.itvAccommodation.setOnWholeItemClickListener(this);
        binding.itvCargoPlatform.setOnWholeItemClickListener(this);
        binding.itvParking.setOnWholeItemClickListener(this);
        binding.itemTvDriver.setOnWholeItemClickListener(this);
        binding.tvTimeUnit.setOnClickListener(v -> {
            if (leaseTimeOpv != null) leaseTimeOpv.show();
        });
//        binding.tvPriceUnit.setOnClickListener(view -> showPriceUnit());
        binding.address.content_edt.setKeyListener(null);
        binding.address.content_edt.setEllipsize(TextUtils.TruncateAt.END);
        binding.address.content_edt.setSingleLine(true);
        binding.itemTvPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        binding.itemTvAcreage.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvAcreage.setCheck(7, 2);
        binding.etNum.setFilters(new EditInputFilter[]{new EditInputFilter(9999999, 1, 0)});
        binding.leaseTime.setFilters(new EditInputFilter[]{new EditInputFilter(9999999, 1, 0)});
        binding.leasePrice.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.llMachineryNumType.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (type != 2) {//不为车辆时
                    showPriceMatchUnitPicker();
                }

            }
        });
        // 租赁时长 ：面议还是报价功能
        binding.rbTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.rbTime.setButtonDrawable(R.mipmap.lease_pub_select);
                    binding.layoutDays.setVisibility(View.GONE);
                    binding.rbTime.setText("您已选择面议，再次点击退出面议");
                } else {
                    binding.rbTime.setButtonDrawable(R.mipmap.lease_pub_unselect);
                    binding.layoutDays.setVisibility(View.VISIBLE);
                    binding.rbTime.setText("面议");
                }
            }
        });
        //   期望租金 ：面议还是报价功能
        binding.rbPrice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.rbPrice.setButtonDrawable(R.mipmap.lease_pub_select);
                    binding.layoutPrice.setVisibility(View.GONE);
                    binding.rbPrice.setText("您已选择面议，再次点击退出面议");
                } else {
                    binding.rbPrice.setButtonDrawable(R.mipmap.lease_pub_unselect);
                    binding.layoutPrice.setVisibility(View.VISIBLE);
                    binding.leasePrice.setText("");
                    binding.leasePrice.setHint("请输入");
                    binding.rbPrice.setText("面议");
                }
            }
        });

        binding.tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                commonEntity.setContactPerson(binding.itemTvName.getContent());
                commonEntity.setContactPhone(binding.itemTvPhone.getContent());
                commonEntity.setRentalCount(binding.etNum.getText().toString().trim());
                commonEntity.setTitle(binding.itemTvTitle.getContent());
//                if (!binding.rbTime.isChecked()) {
                    commonEntity.setRentalDuration(binding.leaseTime.getText().toString().trim());
//                } else {
//                    commonEntity.setRentalDuration("-1");
//                }
                if (!binding.rbPrice.isChecked()) {
                    commonEntity.setRentalPrice(binding.leasePrice.getText().toString().trim());
//                } else {
//                    commonEntity.setRentalPrice("-1");
                }

                if (verifyData()) {
                    String entityJson = GsonManager.getInstance().getGson().toJson(commonEntity);
                    if (type == 4) {//土地
                        AskLeaseLandEntity landEntity = GsonManager.getInstance().getGson().fromJson(entityJson, new TypeToken<AskLeaseLandEntity>() {
                        }.getType());
                        landEntity.setPayMethod(commonEntity.getRentalPayMethod());
                        landEntity.setRentalArea(binding.itemTvAcreage.getContent());
                        model.addOrupdate(landEntity);
                    } else if (type == 5) {//工厂
                        AskLeaseFactoryEntity factoryEntity = GsonManager.getInstance().getGson().fromJson(entityJson, new TypeToken<AskLeaseFactoryEntity>() {
                        }.getType());
                        factoryEntity.setFactoryUse(binding.itvPurpose.getContent().trim());
                        factoryEntity.setRentalArea(binding.itemTvAcreage.getContent().trim());
                        model.addOrupdate(factoryEntity);
                    } else if (type == 6) {//农机
                        AskLeaseFarmEntity farmEntity = GsonManager.getInstance().getGson().fromJson(entityJson, new TypeToken<AskLeaseFarmEntity>() {
                        }.getType());
                        farmEntity.setHasOperator("0".equals(commonEntity.getIsDriver()) ? "1" : "0");//机械设备与农机参数值相反，后端的坑
                        model.addOrupdate(farmEntity);
                    } else {//机械，设备，车辆，房产
                        PublishLeaseDataEntity entity = GsonManager.getInstance().getGson().fromJson(entityJson, new TypeToken<PublishLeaseDataEntity>() {
                        }.getType());
                        PublishLeaseDataEntity.RentAddress rentAddress = new PublishLeaseDataEntity.RentAddress();
                        rentAddress.setProvinceId(commonEntity.getProvinceNo());
                        rentAddress.setCityId(commonEntity.getCityNo());
                        rentAddress.setAreaId(commonEntity.getAreaNo());
                        rentAddress.setTaskaddr(commonEntity.getTaskAddr());
                        rentAddress.setLatitude(commonEntity.getLatitude());
                        rentAddress.setLongitude(commonEntity.getLongitude());
                        entity.setRentAddress(rentAddress);
                        entity.setRentalTitle(commonEntity.getTitle());
                        entity.setRentalArea(binding.itemTvAcreage.getContent());
                        entity.setCategoryParentId(commonEntity.getFirstCategoryNo());
                        entity.setCategoryChildId(commonEntity.getSecondCategoryNo());
                        entity.setRentalSelectPriceId(commonEntity.getPriceUnitNo());
                        entity.setUseAndEnterTime(commonEntity.getUseAndEnterTime());
                        entity.setEndTime(commonEntity.getEndTime());
//                        entity.setRentalUnit(commonEntity.getRentalCountUnit());
                        entity.setRentalUnit(binding.tvNumType.getText().toString());
                        model.addOrupdate(entity);
                    }
                }
            }
        });

    }

    private void init() {
        initData();
        model.getTypeModel();//分类
        model.getModelSelect();//车辆类型
        model.getMechanicalWork();
        model.getSunshineSupply();
        model.getHasNotConfig();
        model.getAddressModel();
    }

    //初始化发票选择数据
    private void initData() {
        //是否带操作员（0:是;1:否）
        driverDatas = new ArrayList<>();
        driverDatas.add(new PickSelectEntity("0", "是"));
        driverDatas.add(new PickSelectEntity("1", "否"));
        commonEntity.setIsDriver("0");
        binding.itemTvDriver.setContent("是");

        priceMatchUnitDatas = new ArrayList<>();
        priceMatchUnitDatas.add(new PickSelectEntity("0", "台"));
        priceMatchUnitDatas.add(new PickSelectEntity("1", "辆"));
        commonEntity.setRentalCountUnit("辆");
        binding.tvNumType.setText("辆");
    }

    //检测收费登录和认证
    public void setDefaultData() {
        if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
            binding.itemTvName.setContent(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, ""));
            binding.itemTvPhone.setContent(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, ""));
        }
    }

    public String getAddressText() {
        return binding.address.getContent();
    }

    public void setAddressText(String addressText, String latitude, String Longitude) {
        commonEntity.setLatitude(latitude);
        commonEntity.setLongitude(Longitude);
        commonEntity.setTaskAddr(addressText);
        binding.address.setContent(addressText);
    }

    private void clearText() {
        binding.itemTvTitle.setContent("");
        binding.itvCategory.setContent("");
        binding.itemTvAcreage.setContent("");
        binding.etNum.setText("");
        binding.leaseTime.setText("");
        binding.rbTime.setChecked(false);
        binding.leasePrice.setText("");
        binding.rbPrice.setChecked(false);
        binding.itvWater.setContent("");
        binding.itvElectricity.setContent("");
        binding.itvSunshine.setContent("");
        binding.itvMachine.setContent("");
        binding.timeCourse.setContent("");
        binding.timeDeadline.setContent("");
        binding.paymentMethod.setContent("");
        binding.itvArea.setContent("");
        binding.itvPurpose.setContent("");
        binding.itvCargoPlatform.setContent("");
        binding.itvOffice.setContent("");
        binding.itvParking.setContent("");
        binding.itvAccommodation.setContent("");
        binding.itemTvDriver.setContent("是");
        commonEntity.setIsDriver("0");
    }

    private boolean verifyData() {
        if (TextUtils.isEmpty(binding.itemTvTitle.getContent())) {
            MToast.showToast(mContext, "请输入您的需求");
            return false;
        }
        if (TextUtils.isEmpty(binding.itvType.getContent())) {
            MToast.showToast(mContext, "请选择需求分类");
            return false;
        }
        if (TextUtils.isEmpty(binding.itvCategory.getContent())) {
            MToast.showToast(mContext, "请选择" + binding.itvCategory.getLabelNameView().getText());
            return false;
        }
//        if (binding.llNum.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.etNum.getText())) {
//            MToast.showToast(mContext, "请输入租赁数量");
//            return false;
//        }
//        if (binding.itemTvAcreage.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itemTvAcreage.getContent())) {
//            MToast.showToast(mContext, "请填写租赁面积");
//            return false;
//        }
//        if (!binding.rbTime.isChecked() && TextUtils.isEmpty(binding.leaseTime.getText().toString())) {
//            MToast.showToast(mContext, "请填写租赁时长");
//            return false;
//        }
//        if (binding.itvPurpose.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvPurpose.getContent())) {
//            MToast.showToast(mContext, "请填写厂房用途");
//            return false;
//        }
//        if (binding.itvWater.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvWater.getContent())) {
//            MToast.showToast(mContext, "请选择供水情况");
//            return false;
//        }
//        if (binding.itvElectricity.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvElectricity.getContent())) {
//            MToast.showToast(mContext, "请选择供电情况");
//            return false;
//        }
//        if (binding.itvSunshine.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvSunshine.getContent())) {
//            MToast.showToast(mContext, "请选择日照情况");
//            return false;
//        }
//        if (binding.itvMachine.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvMachine.getContent())) {
//            MToast.showToast(mContext, "请选择机械作业");
//            return false;
//        }
//        if (binding.itvOffice.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvOffice.getContent())) {
//            MToast.showToast(mContext, "请选择办公区域");
//            return false;
//        }
//        if (binding.itvAccommodation.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvAccommodation.getContent())) {
//            MToast.showToast(mContext, "请选择住宿区域");
//            return false;
//        }
//        if (binding.itvCargoPlatform.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvCargoPlatform.getContent())) {
//            MToast.showToast(mContext, "请选择卸货平台");
//            return false;
//        }
//        if (binding.itvParking.getVisibility() == View.VISIBLE && TextUtils.isEmpty(binding.itvParking.getContent())) {
//            MToast.showToast(mContext, "请选择停车区域");
//            return false;
//        }
//
//        if (TextUtils.isEmpty(binding.itvArea.getContent())) {
//            MToast.showToast(mContext, "请选择使用地区");
//            return false;
//        }
        if (TextUtils.isEmpty(binding.address.getContent())) {
            MToast.showToast(mContext, "请选择工作地");
            return false;
        }
//        if (TextUtils.isEmpty(binding.timeCourse.getContent())) {
//            MToast.showToast(mContext, "请选择" + binding.timeCourse.getLabelNameView().getText());
//            return false;
//        }
//        if (TextUtils.isEmpty(binding.timeDeadline.getContent())) {
//            MToast.showToast(mContext, "请选择报价截止时间");
//            return false;
//        }
//        if (TextUtils.isEmpty(binding.paymentMethod.getContent())) {
//            MToast.showToast(mContext, "请选择付款方式");
//            return false;
//        }
//        if (!binding.rbPrice.isChecked() && TextUtils.isEmpty(binding.leasePrice.getText())) {
//            MToast.showToast(mContext, "请输入期望租金");
//            return false;
//        }
//        if (!binding.rbPrice.isChecked() && Double.parseDouble(binding.leasePrice.getText().toString()) <= 0) {
//            MToast.showToast(mContext, "请填写大于0的期望租金");
//            return false;
//        }
//        if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
//            MToast.showToast(mContext, "请填写联系人");
//            return false;
//        }
//        if (TextUtils.isEmpty(binding.itemTvPhone.getContent())) {
//            MToast.showToast(mContext, "请填写联系电话");
//            return false;
//        }
//        if (!CheckUtils.isPhoneNumberValid(binding.itemTvPhone.getContent())) {
//            MToast.showToast(mContext, "请填写正确的联系电话");
//            return false;
//        }

        if (checkDate(binding.timeDeadline.getContent(), binding.timeCourse.getContent())) {
            return false;
        }

        return true;
    }

    //日期比较
    private boolean checkDate(String startTime, String endTime) {
        try {
            if (!TextUtils.isEmpty(startTime) && !TextUtils.isEmpty(endTime)) {
                if (DateUtils.dateTo13Timestamp(endTime) < DateUtils.dateTo13Timestamp(startTime) || startTime.equals(endTime)) {
                    MToast.showToast(mContext, "报价截止时间需小于" + binding.timeCourse.getLabelNameView().getText());
                    return true;
                }
            }
        } catch (Exception e) {
        }
        return false;
    }


    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.itv_type://需求分类
                onShowType(model.getType());
                break;
            case R.id.itv_category://分类类型
                if (type == 2) {//车辆
                    if (cartypelist.size() > 0) {
                        showcarTypePop();
                    } else {
                        model.getModelSelect();
                    }
                } else {
                    if (classDatas.size() != 0) {
                        if (type == 5) {//工厂
                            showFactoryTypePop();
                        } else {
                            showClassTypePop();
                        }
                    } else {
                        model.getTypeModel();
                    }
                }

                break;
            case R.id.itv_area://地区选择
                if (addressEntityList != null) {
                    showCityPop();
                }
                break;
            case R.id.address://工作点选择
                ZulinGaodeSearchActivity.start(mContext, true, "");
                break;
            case R.id.time_course://使用时间(进场时间)
                showCourseTimePop();
                break;
            case R.id.time_deadline://报价截止时间
                if (TextUtils.isEmpty(binding.timeCourse.getContent())) {
                    MToast.showToast(mContext, "请选择使用时间");
                    return;
                } else {
                    showLimitTimePop();
                }
                break;
            case R.id.payment_method://付款方式选择
                model.getAllPayWay();
                break;
            case R.id.item_tv_driver://选择是否自带操作员
                showDriverPicker();
                break;
            case R.id.itv_water://供水情况
                showWaterPop();
                break;
            case R.id.itv_electricity://供电情况
                showElectriPop();
                break;
            case R.id.itv_sunshine://日照情况
                showSunshinePop();
                break;
            case R.id.itv_machine://机械作业
                showMachinePop();
                break;
            case R.id.itv_office://办公区域
                showOfficePop();
                break;
            case R.id.itv_accommodation://住宿区域
                showAccommodationPop();
                break;
            case R.id.itv_cargo_platform://卸货平台
                showLandingPlatformPop();
                break;
            case R.id.itv_parking://停车区域
                showParkingPop();
                break;
            default:
                break;
        }
    }

    private void onSwitchType(int type) {
        this.type = type;
        commonEntity.setCategoryType(String.valueOf(type));
        binding.itvWater.setVisibility(View.GONE);
        binding.itvElectricity.setVisibility(View.GONE);
        binding.itvSunshine.setVisibility(View.GONE);
        binding.itvMachine.setVisibility(View.GONE);
        binding.itvOffice.setVisibility(View.GONE);
        binding.itvAccommodation.setVisibility(View.GONE);
        binding.itvCargoPlatform.setVisibility(View.GONE);
        binding.itvParking.setVisibility(View.GONE);
        binding.itemTvDriver.setVisibility(View.GONE);
        binding.llNum.setVisibility(View.GONE);
        binding.llNumLine.setVisibility(View.GONE);
        binding.itvType.setVisibility(View.VISIBLE);
        binding.itvCategory.setVisibility(View.VISIBLE);
        binding.tvNumType.setText("辆");
        binding.ivNumType.setVisibility(View.VISIBLE);
        commonEntity.setRentalCountUnit("辆");
        binding.itvPurpose.setVisibility(View.GONE);
        binding.llPrice.setVisibility(View.VISIBLE);
        binding.llTime.setVisibility(View.VISIBLE);
        switch (type) {
            case 0://机械
                binding.itvCategory.setLabelName("机械类型");
                binding.timeCourse.setLabelName("进场时间");
                binding.timeCourse.setLabelHint("请选择进场时间");
                binding.llNum.setVisibility(View.VISIBLE);
                binding.llNumLine.setVisibility(View.VISIBLE);
                binding.itemTvDriver.setVisibility(View.VISIBLE);
                binding.itemTvDriver.setLabelName("是否自带驾驶员");
                binding.itemTvAcreage.setVisibility(View.GONE);
                model.getMachineUnit();
                break;
            case 1://设备
                binding.itvCategory.setLabelName("设备类型");
                binding.timeCourse.setLabelName("进场时间");
                binding.timeCourse.setLabelHint("请选择进场时间");
                binding.llNum.setVisibility(View.VISIBLE);
                binding.llNumLine.setVisibility(View.VISIBLE);
                binding.itemTvDriver.setVisibility(View.VISIBLE);
                binding.itemTvDriver.setLabelName("是否自带操作员");
                binding.itemTvAcreage.setVisibility(View.GONE);
                model.getRentpriceUnit();
                break;
            case 2://车辆
                binding.itvCategory.setLabelName("车辆车型");
                binding.timeCourse.setLabelName("使用时间");
                binding.timeCourse.setLabelHint("请选择使用时间");
                binding.llNum.setVisibility(View.VISIBLE);
                binding.llNumLine.setVisibility(View.VISIBLE);
                binding.itemTvAcreage.setVisibility(View.GONE);
                binding.tvNumType.setText("辆");
                binding.ivNumType.setVisibility(View.GONE);
                commonEntity.setRentalCountUnit("辆");
                model.getNeedLeaseCarPriceUnit();
                break;
            case 3://房产
                binding.itvCategory.setLabelName("房产类型");
                binding.timeCourse.setLabelName("使用时间");
                binding.timeCourse.setLabelHint("请选择使用时间");
                binding.llNum.setVisibility(View.GONE);
                binding.itemTvAcreage.setVisibility(View.VISIBLE);
                model.getEstatePriceHouseUnit();
                break;
            case 4://土地
                binding.itvCategory.setLabelName("土地类型");
                binding.timeCourse.setLabelName("使用时间");
                binding.timeCourse.setLabelHint("请选择使用时间");
                binding.itvWater.setVisibility(View.VISIBLE);
                binding.itvElectricity.setVisibility(View.VISIBLE);
                binding.itvSunshine.setVisibility(View.VISIBLE);
                binding.itvMachine.setVisibility(View.VISIBLE);
                binding.itemTvAcreage.setVisibility(View.VISIBLE);
                model.getPriceUnitLandModel();
                model.getPowerWayLand();
                break;
            case 5://工厂
                binding.itvCategory.setLabelName("工厂分类");
                binding.timeCourse.setLabelName("使用时间");
                binding.timeCourse.setLabelHint("请选择使用时间");
                binding.itvWater.setVisibility(View.VISIBLE);
                binding.itvElectricity.setVisibility(View.VISIBLE);
                binding.itemTvAcreage.setVisibility(View.VISIBLE);
                binding.itvOffice.setVisibility(View.VISIBLE);
                binding.itvAccommodation.setVisibility(View.VISIBLE);
                binding.itvCargoPlatform.setVisibility(View.VISIBLE);
                binding.itvParking.setVisibility(View.VISIBLE);
                binding.itvPurpose.setVisibility(View.VISIBLE);
                model.getPriceUnitSelectFactoryModel();
                model.getPowerWayFactory();
                break;
            case 6://农机
                binding.itvCategory.setLabelName("农机类型");
                binding.timeCourse.setLabelName("使用时间");
                binding.timeCourse.setLabelHint("请选择使用时间");
                binding.llNum.setVisibility(View.VISIBLE);
                binding.llNumLine.setVisibility(View.VISIBLE);
                binding.itemTvAcreage.setVisibility(View.VISIBLE);
                binding.itemTvAcreage.setVisibility(View.GONE);
                binding.itemTvDriver.setVisibility(View.VISIBLE);
                binding.itemTvDriver.setLabelName("是否自带操作员");
                model.getPriceUnitSelectFarmModel();
                break;
        }
    }

    private void onShowType(List<DictEntity3> typeList) {
        OptionsPickerView build = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = typeList.get(options1).getName();
            String no = typeList.get(options1).getId();
            binding.itvType.setContent(content);
            binding.itvCategory.setContent("");
            onSwitchType(Integer.parseInt(no));
            showTypeView(categoryList);
            model.getLeaseTimeUnit(no);
        }).build();
        build.setPicker(typeList);
        build.show();
    }

//    /**
//     * 登录提示
//     */
//    private void showDialogLogin() {
//        new CommonDialog(mContext).builder().setPositiveButton("去登录").setNegativeButton("取消")
//                .setTitle("温馨提示")
//                .setMsg("请先完成登录，并实名认证成功后可进行需求发布操作")
//                .setDialogCallBack(new CommonDialog.DialogCallBack() {
//                    @Override
//                    public void onRightClick() {
//                        VerificationCodeLoginActivity.start(mContext);
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                }).show();
//    }
//
//    /**
//     * 认证提示
//     */
//    private void showDialogAuth() {
//        new CommonDialog(mContext).builder().setPositiveButton("去认证").setNegativeButton("取消")
//                .setTitle("温馨提示")
//                .setMsg("请认证为实名用户/企业用户才可发布需求信息")
//                .setDialogCallBack(new CommonDialog.DialogCallBack() {
//                    @Override
//                    public void onRightClick() {
//                        AuthActivity3.start(mContext);
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                }).show();
//    }

    //发布成功
    public void showSuccess() {
        new CommonDialog(mContext).builder().setPositiveButton("我知道了")
                .setTitle("温馨提示")
                .setMsg("发布成功，请在地图上查看匹配信息，继续完善需求信息请前往用户中心>租赁经营>需求发布")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        clearText();
                        binding.nestedScrollView.scrollTo(0, 0);
                        if (listener != null) {
                            listener.onCallClose(type);
                        }
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    /**
     * 租赁时长
     */
    public void showLeaseTime(List<PickSelectEntity<List<PickSelectEntity>>> list) {
        if (list == null) list = new ArrayList<>();
        leaseTimeDatas.clear();
        leaseTimeDatas.addAll(list);
        leaseTimeOpv = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = leaseTimeDatas.get(options1).getProjectscale();
            String no = leaseTimeDatas.get(options1).getNo();
            binding.tvTimeUnit.setText(content);
            commonEntity.setRentalDurationUnit(content);
            commonEntity.setRentalDurationUnitNo(no);
            binding.tvPriceUnit.setText("元/" + content);
        }).build();
        leaseTimeOpv.setPicker(leaseTimeDatas);
        leaseTimeOpv.setSelectOptions(0);
        if (leaseTimeDatas.size() > 0) {
            commonEntity.setRentalDurationUnit(leaseTimeDatas.get(0).getProjectscale());
            commonEntity.setRentalDurationUnitNo(leaseTimeDatas.get(0).getNo());
            binding.tvTimeUnit.setText(leaseTimeDatas.get(0).getProjectscale());
            binding.tvPriceUnit.setText("元/" + binding.tvTimeUnit.getText());
        }
    }

    //数量单位（辆/台）
    private void showPriceMatchUnitPicker() {
        if (null != priceMatchUnitDatas && priceMatchUnitDatas.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
                String content = priceMatchUnitDatas.get(options1).getProjectscale();
                binding.tvNumType.setText(content);
                commonEntity.setRentalCountUnit(content);
            }).build();
            businessOption.setPicker(priceMatchUnitDatas);
            businessOption.setSelectOptions(1);
            businessOption.show();
        }
    }

    //操作员
    private void showDriverPicker() {
        if (null != driverDatas && driverDatas.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
                String content = driverDatas.get(options1).getProjectscale();
                binding.itemTvDriver.setContent(content);
                commonEntity.setIsDriver(driverDatas.get(options1).getNo());
            }).build();
            businessOption.setPicker(driverDatas);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }

    //供水情况
    private void showWaterPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = powerDatas.get(options1).getPickerViewText();
            commonEntity.setWaterSupply(powerDatas.get(options1).getId());
            commonEntity.setWaterSupplyName(powerDatas.get(options1).getPickerViewText());
            binding.itvWater.setContent(content);
        }).build();
        businessOption.setPicker(powerDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //供电情况
    private void showElectriPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = powerDatas.get(options1).getPickerViewText();
            commonEntity.setPowerSupply(powerDatas.get(options1).getId());
            commonEntity.setPowerSupplyName(powerDatas.get(options1).getPickerViewText());
            binding.itvElectricity.setContent(content);
        }).build();
        businessOption.setPicker(powerDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //日照情况
    private void showSunshinePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = sunshineDatas.get(options1).getPickerViewText();
            commonEntity.setSunshineSupply(sunshineDatas.get(options1).getId());
            commonEntity.setSunshineSupplyName(sunshineDatas.get(options1).getPickerViewText());
            binding.itvSunshine.setContent(content);
        }).build();
        businessOption.setPicker(sunshineDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //机械作业
    private void showMachinePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = machineDatas.get(options1).getPickerViewText();
            commonEntity.setMechanicalWork(machineDatas.get(options1).getId());
            commonEntity.setMechanicalWorkName(machineDatas.get(options1).getPickerViewText());
            binding.itvMachine.setContent(content);
        }).build();
        businessOption.setPicker(machineDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //办公区域
    private void showOfficePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = hasNotList.get(options1).getPickerViewText();
            commonEntity.setOfficeArea(hasNotList.get(options1).getId());
            binding.itvOffice.setContent(content);
        }).build();
        businessOption.setPicker(hasNotList);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //停车区域
    private void showParkingPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = hasNotList.get(options1).getPickerViewText();
            commonEntity.setParkingArea(hasNotList.get(options1).getId());
            binding.itvParking.setContent(content);
        }).build();
        businessOption.setPicker(hasNotList);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //住宿区域
    private void showAccommodationPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = hasNotList.get(options1).getPickerViewText();
            commonEntity.setAccommodationArea(hasNotList.get(options1).getId());
            binding.itvAccommodation.setContent(content);
        }).build();
        businessOption.setPicker(hasNotList);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //卸货平台
    private void showLandingPlatformPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = hasNotList.get(options1).getPickerViewText();
            commonEntity.setLandingPlatform(hasNotList.get(options1).getId());
            binding.itvCargoPlatform.setContent(content);
        }).build();
        businessOption.setPicker(hasNotList);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //分类
    private void showClassTypePop() {
        if (classDatas == null || classDatas.size() == 0 || typeDatas == null || typeDatas.size() == 0) {
            MToast.showToast(mContext, "类型获取失败");
            return;
        }
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String className = classDatas.get(options1).getProjectscale();
            commonEntity.setFirstCategoryNo(classDatas.get(options1).getNo());
            binding.itvCategory.setContent(className);

            if (typeDatas.size() >= 0 && null != typeDatas.get(options1) && typeDatas.get(options1).size() > 0) {
                String typeName = typeDatas.get(options1).get(options2).getProjectscale();
                commonEntity.setSecondCategoryNo(typeDatas.get(options1).get(options2).getNo());
                binding.itvCategory.setContent(className + "/" + typeName);
            }

        }).build();
        businessOption.setPicker(classDatas, typeDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    // 工厂分类的选择
    private void showFactoryTypePop() {
        if (classDatas == null || classDatas.size() == 0) {
            MToast.showToast(mContext, "工厂分类获取失败");
            return;
        }
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String classNamec = classDatas.get(options1).getProjectscale();
            binding.itvCategory.setContent(classNamec);
            commonEntity.setFirstCategoryNo(classDatas.get(options1).getNo());
        }).build();
        businessOption.setPicker(classDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    // 车辆类型的选择
    private void showcarTypePop() {
        if (cartypelist == null || cartypelist.size() == 0) {
            MToast.showToast(mContext, "车型获取失败");
            return;
        }
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String classNamec = cartypelist.get(options1).getProjectscale();
            binding.itvCategory.setContent(classNamec);
            commonEntity.setRentalModelId(cartypelist.get(options1).getNo() + "");
        }).build();
        businessOption.setPicker(cartypelist);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //类型集合
    public void showTypeView(List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> data) {
        this.categoryList = data;
        classDatas = new ArrayList<>();
        typeDatas = new ArrayList<>();
        if (data == null || data.size() == 0) {
            return;
        }
        for (AskLeaseItemEntity<List<AskLeaseItemEntity>> l : data.get(type).getChildren()) {
            PickSelectEntity entity = new PickSelectEntity();
            entity.setNo(l.getNo());
            entity.setProjectscale(l.getName());
            classDatas.add(entity);
            List<PickSelectEntity> childList = new ArrayList<>();
            for (AskLeaseItemEntity ll : l.getChildren()) {
                PickSelectEntity child = new PickSelectEntity();
                child.setNo(ll.getNo());
                child.setProjectscale(ll.getName());
                childList.add(child);
            }
            typeDatas.add(childList);
        }
    }

    //车辆类型数据列表
    public void showCarTypeView(List<DictEntity3> data) {
        for (int i = 0; i < data.size(); i++) {
            PickSelectEntity entity = new PickSelectEntity();
            entity.setNo(data.get(i).getId());
            entity.setProjectscale(data.get(i).getName());
            cartypelist.add(entity);
        }

    }

    //使用时间
    private void showCourseTimePop() {
        selectedDate2 = Calendar.getInstance();
        Calendar dayCalendar = Calendar.getInstance();
        dayCalendar.setTime(new Date());
        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);

        TimePickerView timePicker = new TimePickerBuilder(mContext, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            try {
                binding.timeCourse.setContent(format);
                commonEntity.setUseAndEnterTime(DateUtils.dateTo13Timestamp(format + " 23:59:59") + "");
                selectedDate2.setTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate2);
    }

    //报价截止时间
    private void showLimitTimePop() {
        selectedDate = Calendar.getInstance();
        TimePickerView timePicker = new TimePickerBuilder(mContext, (date, v) -> {
            try {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String format = sdf.format(date);
                if (checkDate(format, binding.timeCourse.getContent())) {
                    return;
                }
                commonEntity.setEndTime(DateUtils.dateTo13Timestamp3(format + " 23:59:59") + "");
                binding.timeDeadline.setContent(format);
                selectedDate.setTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(Calendar.getInstance(), null)
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }


    public void showPowerWay(List<DictEntity3> list) {
        this.powerDatas = list;
    }

    //日照情况
    public void showSunshineSupply(List<DictEntity3> data) {
        this.sunshineDatas = data;
    }

    //机械作业方式
    public void showMechanicalWork(List<DictEntity3> data) {
        this.machineDatas = data;
    }

    public void showPurposeList(List<DictEntity3> data) {
        this.hasNotList = data;
    }

    //支付方式
    public void showAllPayWay(List<DictEntity3> list) {
        this.allpayway = list;
        OptionsPickerView businessOption = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            String content = allpayway.get(options1).getName();
            commonEntity.setRentalPayMethod(allpayway.get(options1).getId());
            binding.paymentMethod.setContent(content);
        }).build();
        businessOption.setPicker(allpayway);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //价格单位
    public void setPriceUnit(List<UnitEntity> list) {
//        this.priceUnit = list;
//        if (list.size() > 0) {
//            binding.tvPriceUnit.setText(list.get(0).getName());
//            commonEntity.setPriceUnitNo(list.get(0).getNo());
//        }
    }

//    //租赁价格单位
//    private void showPriceUnit() {
//        if (priceUnit == null || priceUnit.size() == 0) {
//            return;
//        }
//        OptionsPickerView build = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
//            String content = priceUnit.get(options1).getName();
//            String no = priceUnit.get(options1).getNo();
//            binding.tvPriceUnit.setText(content);
//            commonEntity.setPriceUnitNo(no);
//        }).build();
//        build.setPicker(priceUnit);
//        build.setSelectOptions(0);
//        build.show();
//    }

    //    选择地址弹窗
    private void showCityPop() {
        CityPickView cityOption = new CityPickBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String areaName = "";
                if (opt3.get(options1).get(options2).get(options3) == null) {
                    areaName = "";
                } else {
                    areaName = opt3.get(options1).get(options2).get(options3).getName();
                    cityPosition3 = options3;
                }
                String content = addressEntityList.get(options1).getName() + " / " + opt2.get(options1).get(options2).getName() + " / " + areaName;
                cityPosition1 = options1;
                cityPosition2 = options2;
                binding.itvArea.setContent(content);
//                binding.address.setContent("");

                commonEntity.setProvinceNo(addressEntityList.get(options1).getNo());
                commonEntity.setCityNo(opt2.get(options1).get(options2).getNo());
                commonEntity.setAreaNo(opt3.get(options1).get(options2).get(options3).getNo());
            }
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择地区")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    //返回省市区数据加载数据
    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        addressEntityList = data;
        for (int i = 0; i < addressEntityList.size(); i++) {
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {
                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
    }

    private OnCallBackListener listener;

    public void setOnCallBackListener(OnCallBackListener listener) {
        this.listener = listener;
    }

    public interface OnCallBackListener {
        void onCallClose(int type);
    }
}
